package com.mlm.mlmapp.activity;

import android.content.Intent;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.AQProgressDialog;
import com.mlm.mlmapp.adapter.view.CheckableButton;
import com.mlm.mlmapp.adapter.view.FlowLayout;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
@EActivity(R.layout.dresser_filter)
public class HairDresserFilterActivity extends BaseActivity implements HTTPCons {
	AQuery aq;
	AjaxCallback<JSONObject> postCB;
	AjaxCallback<JSONObject> studioCB;
	private static int POST_VALUE = 1;
	private static int MENDIAN_VALUE = 2;
	@Extra
	String postIDs;
	@Extra
	String studioIDs;
	@ViewById
	FlowLayout flow_layout_post;

	@ViewById
	FlowLayout flow_layout_mendian;

	// @CheckedChange(R.id.checkbox_rating)
	// void ch(CompoundButton btn, Boolean checked) {
	// ratingAsFirstStatus = checked;
	// }
	@Click
	void btn_back() {
		this.finish();
	}

	@Click
	void btn_confirm() {
		Intent intent = new Intent();

		intent.putExtra(HairDresserListActivity.POST_LIST,
				getCheckedID(flow_layout_post));
		intent.putExtra(HairDresserListActivity.STUDIO_LIST,
				getCheckedID(flow_layout_mendian));
//		if (ratingAsFirstStatus) {
//			intent.putExtra(HairDresserListActivity.RATING_DESC, "DESC");
//		} else {
//			intent.putExtra(HairDresserListActivity.RATING_DESC, "");
//		}

		this.setResult(RESULT_OK, intent);
		this.finish();

	}

	@AfterViews
	void init() {
		String title = getTitle().toString();
		if (!"".equals(title)) {
			((TextView) this.findViewById(R.id.tv_title)).setText(title);
		}

		aq = new AQuery(this);
		aq.progress(new AQProgressDialog(this)).ajax(get_dresserlist_post_action, JSONArray.class,
				new AjaxCallback<JSONArray>() {

					@Override
					public void callback(String url, JSONArray object,
										 AjaxStatus status) {
						// TODO Auto-generated method stub
						super.callback(url, object, status);
						if (status.getCode() == -101) {
							Toast.makeText(aq.getContext(), "网络异常,请检查网络",
									Toast.LENGTH_SHORT);
							return;
						}
						if (status.getCode() != 200) {
							Toast.makeText(aq.getContext(),
									getString(R.string.network_error),
									Toast.LENGTH_SHORT);
						} else {
							if (object.length() > 0) {
								for (int i = 0; i < object.length(); i++) {
									try {
										addChildTo(flow_layout_post,
												object.getJSONObject(i),
												POST_VALUE);
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}
						}
					}

				});
		aq.ajax(get_dresserlist_studio_action, JSONArray.class,
				new AjaxCallback<JSONArray>() {

					@Override
					public void callback(String url, JSONArray object,
										 AjaxStatus status) {
						// TODO Auto-generated method stub
						super.callback(url, object, status);
						if (status.getCode() == -101) {
							Toast.makeText(HairDresserFilterActivity.this,
									"网络异常,请检查网络", Toast.LENGTH_SHORT);
							return;
						}
						if (status.getCode() != 200) {
							Toast.makeText(HairDresserFilterActivity.this,
									getString(R.string.network_error),
									Toast.LENGTH_SHORT);
						} else {
							if (object.length() > 0) {
								for (int i = 0; i < object.length(); i++) {
									try {
										addChildTo(flow_layout_mendian,
												object.getJSONObject(i),
												MENDIAN_VALUE);
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}
						}
					}

				});
	}

	private void addChildTo(FlowLayout flowLayout, JSONObject json, int type)
			throws JSONException {


		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		CheckableButton btn = new CheckableButton(this);
		btn.setTextSize(12);
		btn.setTextColor(getResources().getColorStateList(R.color.checkable_text_color));
		btn.setBackgroundResource(R.drawable.checkable_background_with_corner);
		btn.setChecked(false);
		String []array=null;
		if (type == POST_VALUE) {
			if(postIDs.length()>0) {
				if(postIDs.contains(",")) {
					array = postIDs.split(",");

					for (String s : array) {
						if (s.equals(CommonUtils.getJsonString(json, "id"))) {
							btn.setChecked(true);
						}
					}
				}else if(postIDs.equals(CommonUtils.getJsonString(json, "id"))){
					btn.setChecked(true);
				}
			}
			btn.setText(CommonUtils.getJsonString(json, "levlNames"));
			btn.setTag(CommonUtils.getJsonString(json, "id"));
		} else {
			if(studioIDs.length()>0) {
				if(studioIDs.contains(",")) {
					array = studioIDs.split(",");

					for (String s : array) {
						if (s.equals(CommonUtils.getJsonString(json, "id"))) {
							btn.setChecked(true);
						}
					}
				}else if(studioIDs.equals(CommonUtils.getJsonString(json, "id"))){
					btn.setChecked(true);
				}
			}
			btn.setText(CommonUtils.getJsonString(json, "names"));
			btn.setTag(CommonUtils.getJsonString(json, "id"));
		}

//		btn.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				CheckableButton btn = (CheckableButton) v;
//				if (btn.isChecked()) {
//					btn.setTextColor(getResources().getColor(R.color.red));
//
//				} else {
//					btn.setTextColor(getResources()
//							.getColor(R.color.greyborder));
//				}
//			}
//		});
		flowLayout.addView(btn, params);
	}

	private String getCheckedID(FlowLayout layout) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < layout.getChildCount(); i++) {
			CheckableButton btn = (CheckableButton) layout.getChildAt(i);
			if (btn.isChecked()) {
				sb.append(btn.getTag().toString());
				sb.append(",");
			}
		}
		if (sb.length() > 0)
			sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

}
